<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
require_once './sdk/WxpayAPI/lib/WxSend.Api.php';
class AutomaticRemind extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Automaticremind')
            ->setDescription('the automaticremind command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            // 指令输出
//    	$output->writeln('automaticdelivery');
            // 获取自动收货时间
            $time = Db::table('eb_config')->where('name', 'automatic_remind')->value('value');
            if ($time === null) exit;
            $times = $time*60;
            // 自动收货(系统评价)
            $map = '`a.status` = 3 AND `a.is_pay` = 1';
            $page = 1;
            $limit = 1000;
            while (true) {
                $ids = Db::table('eb_order')->alias('a')->leftJoin('eb_admin b', 'a.operate_id = b.id')->leftJoin('eb_user c', 'a.send_id = c.id')->where('a.is_pay',1)->where('a.is_remind',0)->where('a.status',3)->field('a.*,b.delivery_time,c.openid')->page($page, $limit)->select()->toArray();
                if (!$ids) break;
                foreach ($ids as $key=>$value){
                        $a = $value['send_time']+$value['delivery_time']*60-$times;//满额配送时间
                        if(time()>=$a){
                            $send = new \WxSendApi();
                            if(empty($value['send_id'])) break;
                            $ds = json_decode($send->send_rider_remind($value['send_id'],$value),true); //转为数组;
                            $rtn  = Db::table('eb_order')->where('order_no', $value['order_no'])->update(['is_remind' =>1]);
                            if (!$rtn) break;
                            $page++;
                    }else{
                        break;
                    }
                }
                //if (!$rtn) break;
                $page++;
            }
        } catch (\Exception $e) {
            $output->warning($e);
        }
    }
}
